package org.itboys.web.pay;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.PearlAccount;
import org.itboys.mobile.service.mongo.mobile.PearlAccountService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liu on 2017/11/8.
 */
@Controller
@RequestMapping("/sunday/web/pay")
public class PearlAccountController extends BaseController{
    @Autowired
    private PearlAccountService accountService;
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/account/index")
    public String index(HttpServletRequest request,
                        Model model){
        return "/sunday/pay/account/index";
    }

    /**
     * 新增或修改
    */

    @RequestMapping("/account/save")
    public void save(Long accountId,String name,String bank,String account,Integer type,String mobile, String code,HttpServletResponse response){
        try {
            Long memberId = AccountIdHoldUtils.getMemberId();
            accountService.saveAccouunt(accountId,memberId,name,bank,account,type,mobile,code);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 查询账户
     * @param request
     * @param response
     */
    @RequestMapping("/account/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
        //    Map<String,Object> map = new HashMap<>();
            Map<String,Object> param = new HashMap<>();
            param.put("memberId",memberId);


            PageResult<PearlAccount> result=accountService.pageQuery(request,param);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);


        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }
    @RequestMapping("/account/input")
    public String input(HttpServletRequest request,
                        Model model
                       // @RequestParam(value="memberId",required=false)Long memberId
                        ){
        Long memberId = AccountIdHoldUtils.getMemberId();
        PearlAccount pearlAccount = accountService.findOne(memberId);
        model.addAttribute("pearlAccount",pearlAccount);

            return "/sunday/pay/account/input";

    }

}
